<link href="<?php echo MCSS_1;?>/comment/reply.css" rel="stylesheet">
<div class="review_list" id="msg_content">
	<div class="title">用户评价</div>
    <div class="review_ping">
        <div class="com_info clearfix"><span id="recommen_user">XDA网友</span>&nbsp;评论<b>(共<?php echo empty($total) ? 0 : $total; ?>条评论)</b></div>
            <div class="com_con">
                <textarea class="com_mian" name="content" id="content"></textarea>
            </div>
            <div class="post">
                <span id="answer"><img src="<?php echo ROOT; ?>/comment/rand.php" /><input type="text" name="auth" id="auth" /></span>
                <input id="sub_msg" type="button" value="发 表" class="btn_post"/>
            </div>
            <div class="review_list_main clearfix">
                <div class="txtcon_b">
                    <span class ="facessss" id="show_msg_content"></span>
                </div>
            </div>
    </div>
</div>
<div class="review_page" id="pageNav"></div> 
<script>
var args = [];
args['id'] = <?php echo intval($id);?>;
args['url'] = "<?php echo ROOT;?>/comment/";
args['cookiename'] = "<?php echo $comment['cookiename']; ?>";
run(args);

function has_chinese(s){ 
	var patrn=/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi; 
	if(!patrn.exec(s)) return false; 
	else return true; 
} 
function is_legal(c) {
	if(c.indexOf('www.')!==-1 || c.indexOf('url=http://')!==-1 || c.indexOf('http://')!==-1 ||c.indexOf('.com')!==-1) {
		return false;
	}else{
		return true;
	}
}

//取cookies函数       
function getCookie(name) {
  var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
   if(arr != null) return unescape(arr[2]); return null;
}

function show_list(args, pag) {
	$.ajax({
		type:'POST',
		datatype:'json',
		url:args['url']+'index.php',
		data:{'id':args['id'], 'pag':pag},
		success:function(info){
			var data = eval("("+info+")");
            if(data && data.tol!=='0'){
                var list=data.list;   
                var comment = '';
                $.each(list,function(i,n){
                	comment += '<div class="newshow_left_msgfrm_btmline clearfix">';
                	comment += '<div class="newshow_left_msgfrm_left">';
                	comment += '<b color="#006699"><strong>'+n['username']+'</strong></b>于 '+n['ctime']+' 说:</div>';
                    comment += '<div id="user_box_'+n.id+'" class="newshow_left_msgfrm_right">'+n.content+'</div>';
                });

                $("#show_msg_content").html(comment);             
                $("#pageNav").html(data.pager);
				$('div.com_info span.f_l b').html('(共'+data.tol+'条评论)');
            }
		}
	});
}
function check_answer(answer, right, url) {
	if($.trim(answer)==''){
        alert('请输入验证码');
        fresh_rand(url);
        return false;
    }
	if(isNaN(answer)) {
		alert('请输入数字');
		fresh_rand(url);
		clear_answer();
		return false;
	}
    
    if(answer!=void(0)){
        if(right!=answer){
            alert('验证码有误');
            fresh_rand(url);
			clear_answer();
            return false;
        }
    }else return true;
}

function check_content(c, url) {
	if($.trim(c)==''){		
        alert('评论不能为空');
        fresh_rand(url);
        return false;
    }
    if(!is_legal(c)) {
		alert('亲,你填入的信息包括URL字符');
		fresh_rand(url);
		return false;
	}			
	if(c.length>100) {
		alert("亲,留言内容必须在100字以内,请修改长度");
		fresh_rand(url);
		return false;
	}
}
//清空验证码
function clear_answer() {
	$('span#answer input#auth').attr('value', '');
}
//刷新验证码
function fresh_rand(url) {
	var obj = $('div.review_ping div.post span#answer img');
	var href=obj.attr("src");
    $(obj[0]).attr("src",href+"?time="+new Date().getTime());
}

function post_comment(c, answer, args) {
    if(false==check_content(c, args['url'])) return false;
    var right = parseInt(getCookie(args['cookiename']));
    if(false==check_answer(answer, right, args['url'])) return false;
	//获取登录状态
	var username = 'XDA网友';
	var uid = 0;
	$.getJSON("http://login.xda.cn/PPL/dz_login/login_info.php?callback=?",
            function(data){
				 if(data && data.username) {
					 var date = new Date();			
	var cont = '<div style="background:#DFFFDF;" class="newshow_left_msgfrm_btmline clearfix">'+
	'<div class="newshow_left_msgfrm_left">'+
		'<b color="#006699"><strong>'+data.username+'</strong></b>'+
		'于'+date.getFullYear()+'年'+date.getDate()+'月'+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds()+'说：'+
	'</div> <div class="newshow_left_msgfrm_right">'+c+' </div>';

	var reply_num = $('div.txtcon_b span#show_msg_content div.newshow_left_msgfrm_btmline').length;
	if(reply_num>=5) $("div.txtcon_b span#show_msg_content div.newshow_left_msgfrm_btmline:last").fadeOut(); //评论数大于5个 把最后一个隐藏掉

		if(reply_num==0 ) {
			$("div.txtcon_b span#show_msg_content").append(cont);
		}else {
			$("div.txtcon_b span#show_msg_content div.newshow_left_msgfrm_btmline:first").before(cont);
		}
	
	$.post(args['url']+'index.php', {"cfid":args['id'], "uid":data.uid, "username":data.username,"answer":answer, "content":c},function(data){
	},'json');
}else{ //未登录
		$.post(args['url']+'index.php', {"cfid":args['id'], "uid":data.uid, "username":data.username,"answer":answer, "content":c},function(data){
		 //if(!data.success) alert('OMG,出问题了');
		alert('评论已提交,需要后台审核');

	},'json');
	   }
	   $('div.review_ping div.com_con textarea').attr('value', '');//清空内容
		 clear_answer();
	     fresh_rand(args['url']);//刷新验证码
}); 

}

function load_jq(url) {
	if(typeof jQuery !='undefined') return;
	
    //var script = document.createElement("script");
    //script.type = "text/javascript";
    //script.src = url+'jquery.min.js';
    //document.getElementsByTagName("head")[0].appendChild(script);
	document.write('<script type="text/javascript" src="'+url+'jquery.min.js"><\/script>');
}

function loadScript(url,callback) {
    var script=document.createElement("script");
    script.type="text/javascript";
    if(script.readyState){
        script.onreadystatechange=function(){
            if(script.readyState=='loaded'||script.readyState=='complete'){
                script.onreadystatechange=null;
                callback();
            }
        }
    }else{script.onload=function(){callback();}}
    script.src=url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

function main(args) {
	//初始加载第一页
	show_list(args, 1);
	
	//分页
    $("#pageNav a").live("click",function(){
    	var pag = parseInt($(this).attr('rel'));
		show_list(args, pag);
     });
	
  //提交评论
    $('input#sub_msg').click(function() {
    	var c = $('textarea#content').val();
    	var answer = parseInt($.trim($('input#auth').val()));
    	post_comment(c, answer, args);
    });
}

function run(args) {
	if(typeof jQuery!='function') {
		loadScript(args['url']+'jquery.min.js',function(){
			main(args);
		});
	}else{
		$(function(){
			main(args);
		});
	}
}
</script>